<?php

namespace app\sys\controller;

use think\Controller;
use think\Db;

// 后台基类
class Admin extends Controller
{
    public function _initialize()
    {
        $controller = strtolower(request()->controller());
        $action = strtolower(request()->action());
        $url = $controller.'/'.$action;
        // dump(request()->module());
        // 不需要登录的方法
        $no_login = ['index/login', 'index/loginout', 'index/noaccess', 'index/four0four', 'index/getqrcode', 'index/getjoinqrcode', 'index/getjoingangqrcode','order/export'];
        if (in_array($url, $no_login)) {
            return;
        }
        // 开放接口
        $open_api = ['gang/index', 'gang/gangopen','finance/csmanager'];
        $admin_info = session('admin_info');
        if (in_array($url, $open_api) && !$admin_info) {
            $param = input('post.', '');
            $sign = $this->getPaySign($param);
            if (!$param || !$param['sign'] || $sign != $param['sign']) {
                exit('sign错误');
            }
            $admin_info = Db::name('admin')->find($param['admin_id']);
            if (!$admin_info) {
                exit('账号错误');
            }
            session('admin_info', $admin_info);
            $admin_info = session('admin_info');
        }

        if (!$admin_info || !in_array($admin_info['permission'], [3, 6, 10])) {
            redirect()->remember();

            return $this->redirect('index/login')->remember();
        }

        //初始化左侧菜单(能进后台的只有 3客服 6供应商 10管理员)
        $menu = [
            '主页' => ['url' => '/index', 'permission' => [3, 6, 10], 'left_menu' => ['仪表盘' => ['url' => '/index/index', 'permission' => [3, 6, 10]]]],
            '系统设置' => ['url' => '/setting', 'permission' => [10], 'left_menu' => ['公告设置' => ['url' => '/setting/noticelist', 'permission' => [10]], '业务员管理' => ['url' => '/setting/index', 'permission' => [10]], '帮会类别管理' => ['url' => '/setting/gangtype', 'permission' => [10]], '供应商管理' => ['url' => '/setting/supplier', 'permission' => [10]]]],
            '帮会设置' => ['url' => '/gang', 'permission' => [3, 10], 'left_menu' => ['帮会管理' => ['url' => '/gang/index', 'permission' => [3, 10]]]],
            '商品管理' => ['url' => '/product', 'permission' => [6, 10], 'left_menu' => ['商品分类' => ['url' => '/product/producttype', 'permission' => [10]], '商品品牌' => ['url' => '/product/productbrand', 'permission' => [10]], '商品管理' => ['url' => '/product/index', 'permission' => [6, 10]], '售后说明' => ['url' => '/product/aftersale', 'permission' => [6]]]],
            '订单管理' => ['url' => '/order', 'permission' => [6, 10], 'left_menu' => ['订单管理' => ['url' => '/order/index', 'permission' => [6, 10]],'推送绑定' => ['url' => '/order/bindwx', 'permission' => [6, 10]]]],
            '提现记录' => ['url' => '/withdraw', 'permission' => [6, 10], 'left_menu' => ['提现记录' => ['url' => '/withdraw/index', 'permission' => [6, 10]]]],
            '财务管理' => ['url' => '/finance', 'permission' => [3, 6, 10], 'left_menu' => ['财务报表' => ['url' => '/finance/index', 'permission' => [10]], '财务明细' => ['url' => '/finance/detail', 'permission' => [6, 10]], '业务员提成管理' => ['url' => '/finance/csmanager', 'permission' => [3, 10]]]],
            '邮费管理' => ['url' => '/postage', 'permission' => [6, 10], 'left_menu' => ['设置顺丰差价' => ['url' => '/postage/index', 'permission' => [6, 10]], '退货地址' => ['url' => '/postage/returnaddress', 'permission' => [6]]]],
        ];
        foreach ($menu as $key => $tm) {
            if (in_array($admin_info['permission'], $tm['permission'])) {
                $top_menu[$key] = $tm['url'];
                if (0 === strpos($tm['url'], '/'.$controller)) {
                    foreach ($tm['left_menu'] as $k => $m) {
                        if (in_array($admin_info['permission'], $m['permission'])) {
                            $left_menu[$k] = $m['url'];
                        }
                    }
                }
            }
        }
        $this->assign('admin_info', $admin_info);
        $this->assign('left_menu', $left_menu);
        $this->assign('top_menu', $top_menu);
        $top_menu_selected = '/'.$controller;
        $left_menu_selected = '/'.$controller.'/'.$action;
        $this->assign('top_menu_selected', $top_menu_selected);
        $this->assign('left_menu_selected', $left_menu_selected);
        if (!in_array($left_menu_selected, $left_menu) && !request()->isAjax() && !request()->isPost()) {
            if (!$left_menu) {
                return $this->redirect('index/noaccess')->remember();
            }

            return $this->redirect(reset($left_menu))->remember();
        }
        if (request()->isAjax()) {
            $this->view->engine->layout(false);
        }
    }

    public function _empty($name)
    {
        return $this->redirect('index/four0four')->remember();
    }

    private function getPaySign($Obj, $payKey = 'ymqvip123qwe,./')
    {
        if (!$Obj) {
            return false;
        }
        unset($Obj['sign']);
        foreach ($Obj as $k => $v) {
            $Parameters[$k] = $v;
        }
        ksort($Parameters);
        $String = $this->formatBizQueryParaMap($Parameters, false);
        $String = $String.'&key='.$payKey;
        $String = md5($String);
        $result_ = strtoupper($String);

        return $result_;
    }

    private function formatBizQueryParaMap($paraMap, $urlencode)
    {
        $buff = '';
        ksort($paraMap);
        foreach ($paraMap as $k => $v) {
            if ($urlencode) {
                $v = urlencode($v);
            }
            //$buff .= strtolower($k) . "=" . $v . "&";
            $buff .= $k.'='.$v.'&';
        }
        $reqPar;
        if (strlen($buff) > 0) {
            $reqPar = substr($buff, 0, strlen($buff) - 1);
        }

        return $reqPar;
    }
}
